package com.youxin.busfoundation.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.youxin.foundation.utils.StringUtils;

@Configuration
@PropertySource("classpath:${spring.profiles.active}/zfbinfo.properties")
@ConfigurationProperties(prefix = "zfb")
public class ZfbBeanConfig {
	private static  String openApiDomain;
	private static String mcloudApiDomain;
	private static String pid;
	private static String appid;
	private static String privateKey;
	private static String publicKey;
	private static String alipayPublicKey;
	private static String notifyUrl;
	private static int maxQueryRetry;
	private static long queryDuration;
	private static int maxCancelRetry;
	private static long cancelDuration;
	private static long heartbeatDelay;
	private static long heartbeatDuration;
	
	public String getOpenApiDomain() {
		return openApiDomain;
	}

	public void setOpenApiDomain(String openApiDomain) {
		this.openApiDomain = openApiDomain;
	}

	public String getMcloudApiDomain() {
		return mcloudApiDomain;
	}

	public void setMcloudApiDomain(String mcloudApiDomain) {
		this.mcloudApiDomain = mcloudApiDomain;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getPrivateKey() {
		return privateKey;
	}

	public void setPrivateKey(String privateKey) {
		this.privateKey = privateKey;
	}

	public String getPublicKey() {
		return publicKey;
	}

	public void setPublicKey(String publicKey) {
		this.publicKey = publicKey;
	}

	public String getNotifyUrl() {
		return notifyUrl;
	}

	public void setNotifyUrl(String notifyUrl) {
		this.notifyUrl = notifyUrl;
	}

	public String getAlipayPublicKey() {
		return alipayPublicKey;
	}

	public void setAlipayPublicKey(String alipayPublicKey) {
		this.alipayPublicKey = alipayPublicKey;
	}

	public int getMaxQueryRetry() {
		return maxQueryRetry;
	}

	public void setMaxQueryRetry(int maxQueryRetry) {
		this.maxQueryRetry = maxQueryRetry;
	}

	public long getQueryDuration() {
		return queryDuration;
	}

	public void setQueryDuration(long queryDuration) {
		this.queryDuration = queryDuration;
	}

	public int getMaxCancelRetry() {
		return maxCancelRetry;
	}

	public void setMaxCancelRetry(int maxCancelRetry) {
		this.maxCancelRetry = maxCancelRetry;
	}

	public long getCancelDuration() {
		return cancelDuration;
	}

	public void setCancelDuration(long cancelDuration) {
		this.cancelDuration = cancelDuration;
	}

	public long getHeartbeatDelay() {
		return heartbeatDelay;
	}

	public void setHeartbeatDelay(long heartbeatDelay) {
		this.heartbeatDelay = heartbeatDelay;
	}

	public long getHeartbeatDuration() {
		return heartbeatDuration;
	}

	public void setHeartbeatDuration(long heartbeatDuration) {
		this.heartbeatDuration = heartbeatDuration;
	}

	private static ZfbBeanConfig config = null;

	public static void initWithConfig(ZfbBeanConfig zfbConfig) {
		config = zfbConfig;
	}

	public static ZfbBeanConfig getConfig() {
		return config;
	}
	public static String description() {
	    StringBuilder sb = new StringBuilder("Configs{");
	    sb.append("支付宝openapi网关: ").append(openApiDomain).append("\n");
	    if (StringUtils.isNullOrEmpty(mcloudApiDomain)) {
	      sb.append(", 支付宝mcloudapi网关域名: ").append(mcloudApiDomain).append("\n");
	    }
	    if (StringUtils.isNullOrEmpty(pid)) {
	      sb.append(", pid: ").append(pid).append("\n");
	    }
	    sb.append(", appid: ").append(appid).append("\n");

	    sb.append(", 商户RSA私钥: ").append(getKeyDescription(privateKey)).append("\n");
	    sb.append(", 商户RSA公钥: ").append(getKeyDescription(publicKey)).append("\n");
	    sb.append(", 支付宝RSA公钥: ").append(getKeyDescription(alipayPublicKey)).append("\n");

	    sb.append(", 查询重试次数: ").append(maxQueryRetry).append("\n");
	    sb.append(", 查询间隔(毫秒): ").append(queryDuration).append("\n");
	    sb.append(", 撤销尝试次数: ").append(maxCancelRetry).append("\n");
	    sb.append(", 撤销重试间隔(毫秒): ").append(cancelDuration).append("\n");

	    sb.append(", 交易保障调度延迟(秒): ").append(heartbeatDelay).append("\n");
	    sb.append(", 交易保障调度间隔(秒): ").append(heartbeatDuration).append("\n");
	    sb.append("}");
	    return sb.toString();
	  }

	private static String getKeyDescription(String key) {
		int showLength = 6;
		if ((StringUtils.isNullOrEmpty(key)) && (key.length() > showLength)) {
			return key.substring(0, showLength) + "******" + key.substring(key.length() - showLength);
		}
		return null;
	}
}
